package com.lemon.smartsafetynew.ui.fragment.notice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.listener.RecycleChildItemClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafetynew.entity.notice.NoticeInfoVo;
import com.lemon.smartsafetynew.ui.activity.notice.NoticeEditActivity;
import com.lemon.smartsafetynew.ui.adapter.notice.NoticeListAdapter;
import com.thefinestartist.finestwebview.FinestWebView;


import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/7/10.
 */

public class NoticeListFragment extends BasePullToRefreshFragment<NoticeInfoVo, List<NoticeInfoVo>>
        implements RecycleChildItemClickListener<NoticeInfoVo> {

    private Context mContext;
    private int type = 0; // 0：政府公告  1：行业动态   2：内部公告

    @Override
    public void onPullRefresh() {
        //TODO 下拉刷新数据
        Log.i("yxy", "onPullRefresh");
//        SafeDataServices.init()
//                .getAllBuildingList(RefreshType.REFRESH, pageSize, currentPage, mCompanyInfo.getCompany_code(), mGroupSelectValue, mSearchText);
    }

    @Override
    public void onLoadMore() {
        //TODO 这里会在下拉刷新后，才会被允许
        Log.i("yxy", "onLoadMore");
//        SafeDataServices.init()
//                .getAllBuildingList(RefreshType.LOAD_MORE, pageSize, currentPage, mCompanyInfo.getCompany_code(), mGroupSelectValue, mSearchText);
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        if (getArguments() != null) {
            //TODO 增加用户的角色，权限的判断
            type = getArguments().getInt(BundleKey.KEY_WORD);
        }

    }

    public static NoticeListFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt(BundleKey.KEY_WORD, type);

        NoticeListFragment fragment = new NoticeListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public boolean goRefreshOnCreate() {
        //TODO
        return false;
    }

    @Override
    public LemonRcvAdapter<NoticeInfoVo> getLemonAdapter() {
        return new NoticeListAdapter(mDataList, this, type);
    }

    @Override
    protected void initViews() {
        super.initViews();

        mContext = getActivity();


        // ==== 内部公告 ====
        if(type == 2) {
            if(fab != null) {
                fab.setImageResource(R.mipmap.edit);
                fab.setVisibility(View.VISIBLE);
                fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent mIntent = new Intent(mContext, NoticeEditActivity.class);
                        mContext.startActivity(mIntent);
                    }
                });
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        NoticeInfoVo noticeInfoVo1 = new NoticeInfoVo();
        noticeInfoVo1.setTitle("this is y1");
        mDataList.add(noticeInfoVo1);

        NoticeInfoVo noticeInfoVo2 = new NoticeInfoVo();
        noticeInfoVo2.setTitle("this is y2");
        mDataList.add(noticeInfoVo2);

        NoticeInfoVo noticeInfoVo3 = new NoticeInfoVo();
        noticeInfoVo3.setTitle("this is y3");
        mDataList.add(noticeInfoVo3);

        NoticeInfoVo noticeInfoVo4 = new NoticeInfoVo();
        noticeInfoVo4.setTitle("this is y4");
        mDataList.add(noticeInfoVo4);

        NoticeInfoVo noticeInfoVo5 = new NoticeInfoVo();
        noticeInfoVo5.setTitle("this is y5");
        mDataList.add(noticeInfoVo5);

        NoticeInfoVo noticeInfoVo6 = new NoticeInfoVo();
        noticeInfoVo6.setTitle("this is y6");
        mDataList.add(noticeInfoVo6);

        NoticeInfoVo noticeInfoVo7 = new NoticeInfoVo();
        noticeInfoVo7.setTitle("this is y7");
        mDataList.add(noticeInfoVo7);

        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public String getHttpTag() {
        return null;
    }


    @Override
    public void onItemClick(NoticeInfoVo data, int position) {

        new FinestWebView.Builder(getActivity()).theme(R.style.FinestWebViewTheme)
                .showUrl(false)
                .statusBarColorRes(R.color.colorPrimaryDark)
                .toolbarColorRes(R.color.colorPrimary)
                .titleColorRes(R.color.white)
                .iconDefaultColorRes(R.color.finestWhite)
                .progressBarColorRes(R.color.finestWhite)
                .showSwipeRefreshLayout(true)
                .swipeRefreshColorRes(R.color.colorPrimaryDark)
                .menuSelector(R.drawable.selector_light_theme)
                .menuTextGravity(Gravity.CENTER)
                .menuTextPaddingRightRes(R.dimen.defaultMenuTextPaddingLeft)
                .dividerHeight(0)
                .gradientDivider(false)
                .setCustomAnimations(R.anim.slide_left_in, R.anim.hold, R.anim.hold, R.anim.slide_right_out)
                .showMenuShareVia(false)
                .stringResCopiedToClipboard(R.string.web_view_copied)
                .stringResCopyLink(R.string.web_view_copy)
                .stringResOpenWith(R.string.web_view_open)
                .show("http://blog.csdn.net/lixin84915/article/details/8148920");

    }

    @Override
    public void onChildItemClick(NoticeInfoVo data, int position) {
        Log.e("yxy", "onChildItemClick " +  position);

        //正常的逻辑应该是，网络请求，请求成功后，再次刷新
        if(position != 0) {
            //TODO 网络请求
            mDataList.remove(position);
            mDataList.add(0, data);
            mAdapter.notifyDataSetChanged();
            mPullToRefreshRecyclerView.smoothScrollToPosition(0);
            for (NoticeInfoVo n : mDataList) {
                Log.e("yxy", "title " + n.getTitle());
            }
        }
    }

}
